package com.ss.android.article.base.feature.detail.presenter;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;

import com.bytedance.common.utility.Logger;
import com.ss.android.article.base.app.BizApplication;
import com.ss.android.article.base.feature.app.b.DBHelper;
import com.ss.android.article.base.feature.detail.model.ArticleDetail;
import com.ss.android.article.base.feature.detail.model.ArticleInfo;
import com.ss.android.article.base.feature.feed.presenter.ArticleQueryThread;
import com.ss.android.article.base.feature.model.Article;
import com.ss.android.common.load.AsyncLoader;
import com.ss.android.model.AbsArticle;
import com.ss.android.newmedia.f.Response;

import org.json.JSONObject;

import java.lang.ref.WeakReference;

public final class DetailLoader {
    public interface Callback {
        void onLoaded(Article article, AbsArticle absArticle, ArticleDetail articleDetail);

        void onCommentLoaded(CommentFetcher arg1, boolean arg2);

        void onArticleInfoLoaded(Article article, ArticleInfo articleInfo);

        void onArticleDetailLoadedWithCache(Article article, ArticleDetail articleDetail);

        void onLoaded(String arg1, long arg2, Response response);

        void onArticleDetailLoadedFromInternet(String arg1, Article article, ArticleDetail articleDetail);
    }

    Context mContext;
    public JSONObject mHttpRequestHeaders;
    public String mUserAgent;
    public long ad_id;
    public int article_page;
    WeakReference<Callback> mDetailLoaderCallbackRef;
    public boolean useV16;
    public AsyncLoader mArticleDetailAsyncCacheLoader;
    private DBHelper mDBHelper;
    private String mFromCategory;
    private int flags;
    private Handler mHandler;
    private AsyncLoader mArticleDetailAsyncLoader;
    private AsyncLoader.LoaderProxy mArticleDetailLoader;
    private AsyncLoader mArticleDetailAsyncDBLoader;
    private AsyncLoader.LoaderProxy mArticleDetailDBLoader;
    private AsyncLoader.LoaderProxy mArticleDetailCacheLoader;
    private AsyncLoader mCommentAsyncLoader;
    private AsyncLoader.LoaderProxy mCommentLoader;
    private AsyncLoader mArticleInfoAsyncLoader;
    private AsyncLoader.LoaderProxy mArticleInfoLoader;
    private AsyncLoader mCommonAsyncLoader;
    private AsyncLoader.LoaderProxy mCommonLoader;

    public DetailLoader(String fromCategory, String userAgent, long ad_id, Callback detailLoaderCallback, int flags) {
        this.mArticleDetailLoader = new ArticleDetailLoader(this);
        this.mArticleDetailDBLoader = new ArticleDetailDBLoader(this);
        this.mArticleDetailCacheLoader = new ArticleDetailCacheLoader(this);
        this.mCommentLoader = new CommentLoader(this);
        this.mArticleInfoLoader = new ArticleInfoLoader(this);
        this.mCommonLoader = new CommonLoader(this);
        this.mContext = BizApplication.getApplication();
        this.mDBHelper = DBHelper.getInstance(this.mContext);
        this.mHandler = new Handler(Looper.getMainLooper());
        this.flags = flags;
        this.mFromCategory = fromCategory;
        this.mUserAgent = userAgent;
        this.mHttpRequestHeaders = null;
        this.ad_id = ad_id;
        this.mDetailLoaderCallbackRef = new WeakReference(detailLoaderCallback);
        this.mArticleDetailAsyncLoader = new AsyncLoader(this.mArticleDetailLoader);
        this.mArticleDetailAsyncDBLoader = new AsyncLoader(6, 1, this.mArticleDetailDBLoader);
        this.mArticleDetailAsyncCacheLoader = new AsyncLoader(6, 2, this.mArticleDetailCacheLoader);
        this.mCommonAsyncLoader = new AsyncLoader(6, 1, this.mCommonLoader);
        this.mArticleInfoAsyncLoader = new AsyncLoader(4, 1, this.mArticleInfoLoader);
        this.mCommentAsyncLoader = new AsyncLoader(4, 1, this.mCommentLoader);
    }

    final ArticleInfo getArticleInfo(Article article, String from, int article_page, boolean useV16) {
        if (article == null) {
            return null;
        }

        String from_category = this.mFromCategory;
        try {
            return ArticleQueryThread.getArticleInfo(this.mDBHelper,
                    article, this.ad_id,
                    from_category, from,
                    this.flags, article_page,
                    useV16);
        } catch (Throwable throwable) {
            return null;
        }
    }

    final ArticleDetail getArticleDetailWithCache(Article article, String ifNoneMatch) {
        try {
            return ArticleQueryThread.getArticleDetail(this.mDBHelper, article, false, ifNoneMatch);
        } catch (Throwable throwable) {
            Logger.w("DetailLoader", "get article detail exception: " + throwable);
            return null;
        }
    }

    final ArticleDetail getArticleDetailWithNoCache(AbsArticle absArticle, boolean arg8) {
        if (absArticle != null && absArticle.groupId >= 0) {
            try {
                return ArticleQueryThread.getArticleDetail(this.mDBHelper, absArticle, arg8, null);
            } catch (Throwable throwable) {
                Logger.w("DetailLoader", "get article detail exception: " + throwable);
            }
        }
        return null;
    }

    public final void onResume() {
        if (this.mArticleDetailAsyncDBLoader != null) {
            this.mArticleDetailAsyncDBLoader.resume();
        }

        if (this.mArticleDetailAsyncLoader != null) {
            this.mArticleDetailAsyncLoader.resume();
        }

        if (this.mArticleDetailAsyncCacheLoader != null) {
            this.mArticleDetailAsyncCacheLoader.resume();
        }

        if (this.mCommentAsyncLoader != null) {
            this.mCommentAsyncLoader.resume();
        }

        if (this.mArticleInfoAsyncLoader != null) {
            this.mArticleInfoAsyncLoader.resume();
        }

        if (this.mCommonAsyncLoader != null) {
            this.mCommonAsyncLoader.resume();
        }
    }

    final void onLoaded(Article article, AbsArticle absArticle, ArticleDetail articleDetail) {
        Callback callback = this.mDetailLoaderCallbackRef.get();
        if (callback != null) {
            callback.onLoaded(article, absArticle, articleDetail);
        }
    }

    public final void commonRequest(String arg5, long arg6) {
        this.mCommonAsyncLoader.addTask(arg5, Long.valueOf(arg6), null, null);
    }

    public final void loadComment(String arg3, CommentFetcher arg4) {
        this.mCommentAsyncLoader.addTask(arg3, arg4, null, null);
    }

    public final void loadArticleDetail(String itemKey, Article article, AbsArticle absArticle) {
        this.mArticleDetailAsyncLoader.addTask(itemKey, article, absArticle, null);
    }

    public final void loadArticleInfo(String itemKey, Article article, String fromWhere) {
        this.mArticleInfoAsyncLoader.addTask(itemKey, article, fromWhere, null);
    }

    final ArticleDetail queryArticleDetail(AbsArticle absArticle, boolean fillArticle) {
        try {
            return this.mDBHelper.queryArticleDetail(absArticle, fillArticle);
        } catch (Exception exception) {
            return null;
        }
    }

    public final void onPause() {
        if (this.mArticleDetailAsyncDBLoader != null) {
            this.mArticleDetailAsyncDBLoader.pause();
        }

        if (this.mArticleDetailAsyncLoader != null) {
            this.mArticleDetailAsyncLoader.pause();
        }

        if (this.mArticleDetailAsyncCacheLoader != null) {
            this.mArticleDetailAsyncCacheLoader.pause();
        }

        if (this.mCommentAsyncLoader != null) {
            this.mCommentAsyncLoader.pause();
        }

        if (this.mArticleInfoAsyncLoader != null) {
            this.mArticleInfoAsyncLoader.pause();
        }

        if (this.mCommonAsyncLoader != null) {
            this.mCommonAsyncLoader.pause();
        }
    }

    public final void loadArticleDetailByLocal(String arg5, final Article article, final AbsArticle absArticle) {
        boolean hasCache = false;
        final ArticleDetail articleDetail = android.support.design.a.getDetailCache(arg5);
        if (articleDetail != null) {
            if (article == null && articleDetail.article == null) {
                hasCache = false;
            } else {
                hasCache = true;
            }
            if (!hasCache) {
                this.mArticleDetailAsyncDBLoader.addTask(arg5, article, absArticle, null);
            } else {
                this.mHandler.post(new Runnable() {

                    public final void run() {
                        DetailLoader.this.onLoaded(article, absArticle, articleDetail);
                    }
                });
            }
        } else {
            this.mArticleDetailAsyncDBLoader.addTask(arg5, article, absArticle, null);
        }
    }

    public final void onDestroy() {
        if (this.mArticleDetailAsyncDBLoader != null) {
            this.mArticleDetailAsyncDBLoader.stop();
        }

        if (this.mArticleDetailAsyncLoader != null) {
            this.mArticleDetailAsyncLoader.stop();
        }

        if (this.mArticleDetailAsyncCacheLoader != null) {
            this.mArticleDetailAsyncCacheLoader.stop();
        }

        if (this.mCommentAsyncLoader != null) {
            this.mCommentAsyncLoader.stop();
        }

        if (this.mArticleInfoAsyncLoader != null) {
            this.mArticleInfoAsyncLoader.stop();
        }

        if (this.mCommonAsyncLoader != null) {
            this.mCommonAsyncLoader.stop();
        }
    }
}

